package com.softmaker.controller;

import com.softmaker.entity.UserInfo;
import com.softmaker.entity.UserPoint;
import com.softmaker.service.MSGService;
import com.softmaker.service.UserInfoService;
import com.softmaker.utils.RandomUtil;
import com.softmaker.utils.ResultVOUtil;
import com.softmaker.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService service;
    @Autowired
    private MSGService msgService;
    @PostMapping ("/addUser")
    public Result<String> addUser(@RequestBody UserInfo userInfo){
        System.out.println(userInfo.getUserName().length());
        if(userInfo.getUserName().length()<8 || userInfo.getUserName().length()>12){
            return ResultVOUtil.error("用户名长度需要大于8位小于12位");
        }
        if(userInfo.getPassword().length()<8 || userInfo.getPassword().length()>12){
            return ResultVOUtil.error("密码长度需要大于8位小于12位");
        }
        if(service.findUser(userInfo)!=null){
            return ResultVOUtil.error("用户名已存在");
        }
        service.addStudent(userInfo);
        return ResultVOUtil.success("成功");
    }
    @PostMapping("/getUser")
    public Result<UserInfo> getUser(@RequestBody UserInfo userInfo){
        UserInfo userInfos = service.findUser(userInfo);
        if(userInfos == null){
            return ResultVOUtil.error("用户不存在");
        }
        if(!userInfo.getPassword().equals(userInfos.getPassword())){
            return ResultVOUtil.error("密码错误");
        }
        return ResultVOUtil.success(userInfos);
    }
    @PostMapping("/delUser")
    public Result<String> deleteUser(@RequestBody UserInfo userInfo){
        if(userInfo == null || userInfo.getUserName() == null || service.findUser(userInfo)==null){
            return ResultVOUtil.error("用户不存在");
        }
        service.deleteUserInfoByUserName(userInfo.getUserName());
        return ResultVOUtil.success("成功");
    }
    @PostMapping("/updUser")
    public Result<UserInfo> updateUser(@RequestBody UserInfo userInfo){
        if(userInfo == null || userInfo.getUserName() == null || service.findUser(userInfo)==null){
            return ResultVOUtil.error("用户不存在");
        }
        UserInfo userInfo1=service.updatePasswordByUserName(userInfo);
        return ResultVOUtil.success(userInfo1);
    }
    @GetMapping("/listUser")
    public Result<Page> listUser(@RequestParam(value="pageNum", required = false,defaultValue = "1") Integer pageNum, @RequestParam(value="pageSize", required = false,defaultValue = "2") Integer pageSize){
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        return ResultVOUtil.success(service.listUser(pageable));
    }
    @PostMapping("/getCode")
    public Result<String> getCode(@RequestParam(value = "phone") String phone){
        String code = RandomUtil.getFourBitRandom();
        Map map = new HashMap();
        map.put("code",code);
        boolean b = msgService.send(map,phone);
        return ResultVOUtil.success(code);
    }
    @PostMapping("/addUserCount")
    public Result<String> addUserCount(@RequestBody UserPoint userPoint){
        service.addUserCount(userPoint);
        return ResultVOUtil.success("成功");
    }
    @PostMapping("/getUserCount")
    public Result<Integer> getUserCount(@RequestBody UserPoint userPoint){
        return ResultVOUtil.success(service.getUserCount(userPoint.getUserName()));
    }
    @PostMapping("/userCheckin")
    public Result<String> userCheckin(@RequestBody UserPoint userPoint){
        LocalDate localDate = LocalDate.now().minusDays(1);
        ZoneId zoneId = ZoneId.systemDefault();
        Date date = Date.from(localDate.atStartOfDay().atZone(zoneId).toInstant());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String datef = sdf.format(date);
        if(!datef.equals(service.getCheckTime(userPoint.getUserName()))) {
            service.addCheckTime(userPoint.getUserName(), datef);
            service.addUserCount(userPoint);
            return ResultVOUtil.success("成功");
        }else{
            return ResultVOUtil.error("今日已签到");
        }
    }
    @GetMapping("/getVideoNameByUserId")
    public Result<List<String>> getVideoIdByUserId(@RequestParam(value = "userId") String userId){
        return ResultVOUtil.success(service.getVideoNameByUserId(userId));
    }
}
